package com.gitee.beiding.cloud.easy_call.entity;

import com.google.common.base.Joiner;
import lombok.Getter;
import lombok.Setter;

import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;

@Getter
@Setter
public class BeanWrapper {

    private TypeMapper typeMapper;

    private Object bean;

    private Map<String, Method> allowMethods = new HashMap<>();

    private String toMethodName(String name, List<String> paramTypes) {
        String join = Joiner.on(",").join(paramTypes);
        return name + ":" + join;
    }

    public Method getMethod(String name) {
        return getMethod(name, Collections.EMPTY_LIST);
    }

    //获取对应的方法
    public Method getMethod(String name, List<String> paramTypes) {
        paramTypes = paramTypes.stream().map(s -> {
            String byName = typeMapper.getClassByName(s);
            return byName == null ? s : byName;
        }).collect(Collectors.toList());
        String s = toMethodName(name, paramTypes);
        return allowMethods.get(s);
    }

    public void allowMethod(Method method) {
        Class<?>[] types = method.getParameterTypes();
        allowMethods.put(toMethodName(method.getName(), Arrays.stream(types).map(Class::getName).collect(Collectors.toList())), method);
    }

}
